/*
 *
 *
 *
 */
package com.cms.template.directive;

import com.cms.entity.Site;
import com.cms.entity.Tag;
import com.cms.service.TagService;
import freemarker.core.Environment;
import freemarker.template.TemplateDirectiveBody;
import freemarker.template.TemplateException;
import freemarker.template.TemplateModel;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.util.List;
import java.util.Map;

/**
 * 模板指令 - 标签列表
 *
 *
 *
 */
@Component
public class TagListDirective extends BaseDirective {

    @Autowired
    private TagService tagService;

	/** 变量名称 */
	private static final String VARIABLE_NAME = "tags";


    @SuppressWarnings({ "unchecked", "rawtypes" })
    public void execute(Environment env, Map params, TemplateModel[] loopVars, TemplateDirectiveBody body) throws TemplateException, IOException {
        // TODO Auto-generated method stub
        Site currentSite = getCurrentSite();
        Integer siteId = currentSite.getId();
        Integer start = getStart(params);
        Integer count = getCount(params);
        String condition = getCondition(params);
        String orderBy = getOrderBy(params);
        List<Tag> tags = tagService.findList(condition,orderBy, start,count,siteId);
        setLocalVariable(VARIABLE_NAME, tags, env, body);
    }
}